#!/usr/bin/env python
'''a game call m12
'''
import random
class M12:
    '''the sporadic groups
    Mathieu groups M12
    '''
    def __init__(self):
        self.lst = range(1, 13)
        self.last_lst = []
        self.custom = {
    "x1":"IM2IM5IM4",
    #3 in[4,5,10]:x1
    "x2":"IMIM3IM2",
    #3 in[6,7,8,11,12]:x2
    #3 in [9]:x2->3 in 4
    "y1":"IMIM3IM2IM9IM7IM8",
    #4 in [6,7,12]:y1
    #4 in [5,9,10,11]:y1->4 in 8
    "y2":"IM3IM6IMIM9IM7IM8",
    #4 in [8]:y2->4 in 12
    "z1":"IM9IM7IM8IM7IMIM5",
    #5 in [7,10,12]:z1
    "z2":"IMIM3IM2IM7IMIM5IM7IMIM5",
    #5 in [6,8,9,11]:z1->5 in 6:z2
    }


    def reset(self):
        '''change back'''
        self.lst = range(1, 13)

    def invert(self):
        '''[1,2,3] to [3,2,1]'''
        tmp = []
        for i in range(11, -1, -1):
            tmp += [self.lst[i]]
        self.lst = tmp

    def merge(self, num = 1):
        '''[1,2,3,4] to [1,4,2,3]'''
        for i in range(num):
            tmp = []
            for i in range(11, 5, -1):
                tmp += [self.lst[i]]
            tmp2 = []
            for i in range(0, 6):
                tmp2 += [self.lst[i], tmp[i]]
            self.lst = tmp2

    def show(self):
        '''show'''
        print self.lst

    def undo(self):
        '''undo'''
        self.lst = self.last_lst[-2]

    def randomize(self):
        '''randomize'''
        self.reset()
        num = 0
        while num < 200:
            num += 1
            tmp = random.random()
            if tmp > 0.67:
                self.merge()
            else:
                self.invert()

    def run_cus(self, str1):
        '''run custom '''
        if str1 == "":
            return
        num_a = str1[0]
        if num_a == 'I':
            self.invert()
            tmp = 1
        if  num_a == 'M':
            num_b = str1[1]
            if num_b in [chr (i) for i in range(ord('1'), ord('9')+1)]:
                num = int(num_b)
                self.merge(num)
                tmp = 2
            else:
                self.merge()
                tmp = 1
        self.run_cus(str1[tmp:])

    def loop(self):
        '''get input'''
        while  1:
            self.last_lst += [self.lst]
            str_1 = raw_input("What is your choice:   ")
            if len(str_1) == 1:
                if str_1 == 'r':
                    self.randomize()
                if str_1 == 'm':
                    self.merge()
                if str_1 == 'i':
                    self.invert()
                if str_1 == 'e':
                    self.reset()
                if str_1 == 'u':
                    self.undo()
                if str_1 == 'Q':
                    break

            elif len(str_1) == 2:
                num = int(str_1[1])
                self.merge(num)

            elif len(str_1) == 3:
                str_2 = str_1[1:]
                if str_2 in self.custom:
                    self.run_cus(self.custom[str_2])

            self.show()


def main():
    '''main'''
    m12 = M12()
    m12.loop()

if __name__ == '__main__':
    main()
